package com.seu.me.test.api;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * Created by liangfeihu on 2018/5/5.
 */
public class WeatherApi {
    public static void main(String[] args) throws Exception {
        String url = "http://wthrcdn.etouch.cn/weather_mini?city=北京";
        // 使用HttpClient，支持GZIP
        RestTemplate restTemplate = new RestTemplate(
                new HttpComponentsClientHttpRequestFactory());
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(StandardCharsets.UTF_8));
        String object = restTemplate.getForObject(url, String.class);
        System.out.println(object);
        JSONObject jsonObject = JSONObject.parseObject(object);
        JSONObject weather = jsonObject.getJSONObject("data");
        System.out.println(weather.getString("city"));
        System.out.println(weather.getString("ganmao"));

        JSONObject forecast = weather.getJSONArray("forecast").getJSONObject(0);
        forecast.put("city", weather.getString("city"));
        forecast.put("ganmao", weather.getString("ganmao"));
        forecast.put("wendu", weather.getString("wendu"));
        forecast.put("aqi", weather.getString("aqi"));

        System.out.println(forecast.toJSONString());

        System.out.println(java.net.URLEncoder.encode("上海", "UTF-8"));

    }
}
